﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MODEL;
using System.Drawing;
using EncryptionServer.DogHelper;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using BLL;
using COM;

namespace EncryptionServer.DogHelper
{
    public class SocketMethod
    {
        public static string strEnWebDogCode;
        Dog dog = Dog.GetInstance;
        private const string GETDOGCODE = "GD";//传输命令
        picMakeUpBLL picBLL = new picMakeUpBLL();
        /// <summary>
        /// 解密XY坐标
        /// </summary>
        /// <param name="boardId"></param>
        /// <returns></returns>
        public string DecEnstrypt(int boardId)
        {
            StringBuilder sb = new StringBuilder();
            //WriteLogForCheck.WriteLogFile("准备获取大板" + boardId + "picMakeup");
            List<PicMakeUpInfo> listPicMakeUp = GetlistPicMakeUp(boardId);
            //WriteLogForCheck.WriteLogFile("获取大板" + boardId + "picMakeup成功");
            //FrmMain.dogMutex.WaitOne();
            try
            {
                foreach (var item in listPicMakeUp)
                {
                   // WriteLogForCheck.WriteLogFile("准备解密照片" + item.Id + "x坐标");
                    string x = dog.DeciperXY(item.X).Substring(0, item.X.Length);
                    //WriteLogForCheck.WriteLogFile("解密照片" + item.Id + "x坐标成功");
                    //WriteLogForCheck.WriteLogFile("准备解密照片" + item.Id + "y坐标");
                    string y = dog.DeciperXY(item.Y).Substring(0, item.Y.Length);
                    //WriteLogForCheck.WriteLogFile("解密照片" + item.Id + "y坐标成功");
                    sb.Append(item.Id);
                    sb.Append("○");
                    sb.Append(x);
                    sb.Append("○");
                    sb.Append(y);
                    sb.Append("¤");
                }
            }
            finally
            {
                //FrmMain.dogMutex.ReleaseMutex();
            }

            return sb.ToString();
        }

        private List<PicMakeUpInfo> GetlistPicMakeUp(int boardid)
        {
            try
            {
                return picBLL.GetPicMakeUpList(boardid);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return null;
        }
        public void DecSocket()
        {
            TcpListener lisener = new TcpListener(IPAddress.Any,EncryptionServer.Properties.Settings.Default.deciperServerPort);
            lisener.Start();
            byte [] buffer = new byte[1024];
            byte[] sendBytes = null;
            string recData;
            string sendData;
            Socket sk = null;
            while (true)
            {
                try
                {
                    sk = lisener.AcceptSocket();

                    int ret = sk.Receive(buffer);
                    recData = System.Text.Encoding.Unicode.GetString(buffer,0,ret);
                    if (recData == GETDOGCODE)
                    {
                        sendData = string.IsNullOrEmpty(strEnWebDogCode) ? "" : strEnWebDogCode;
                    }
                    else
                    {
                        //WriteLogForCheck.WriteLogFile("准备解密大板" +recData);
                        sendData = DecEnstrypt(Convert.ToInt32(recData));
                        //WriteLogForCheck.WriteLogFile("解密大板" + recData+"成功");
                    }
                    sendBytes = System.Text.Encoding.Unicode.GetBytes(sendData);
                    //WriteLogForCheck.WriteLogFile("准备发送大板" + recData+"解密后字符串");
                    sk.Send(sendBytes);
                    //WriteLogForCheck.WriteLogFile("发送大板" + recData + "解密后字符串成功");
                    sk.Close();
                }
                catch (Exception ex)
                {
                    WriteLogForCheck.WriteLogFile("解密时出错:" + ex.Message + DateTime.Now);
                }
                finally
                {
                    if (sk != null)
                    {
                        sk.Close();
                    }
                }
            }
          
        }
    }
}
